import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-05-13
 * Time: 17:03
 */
public class Main {
    //day6 排序 力扣2418 按身高排序

    //方法一：利用哈希存下映射关系
    public String[] sortPeople(String[] names, int[] heights) {
        //1.先用map将身高与名字建立映射关系
        Map<Integer, String> map = new HashMap<>();
        int n = names.length;
        for (int i = 0; i < n; i++) {
            map.put(heights[i], names[i]);
        }
        //2.对身高进行排序
        Arrays.sort(heights);
        //通过map将身高排序后对应的名字提取出来
        int index = 0;
        String[] str = new String[n];
        for(int i = heights.length-1;i >= 0;i--) {
            str[index++] = map.get(heights[i]);
        }
        return str;
    }


    //方法二：对下标进行排序(不改变数组中的元素位置)
    public String[] sortPeople1(String[] names, int[] heights) {
        int n = names.length;
        // 1.创建下标数组
        Integer[] index = new Integer[n];
        for (int i = 0; i < n; i++) {
            index[i] = i;
        }
        // 2.对下标数组排序(排序的规则是根据身高数组降序)
        Arrays.sort(index, (i, j) -> {
            return heights[j] - heights[i];
        });
        // 3.遍历下标数组的值作为names数组的下标传给新数组
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = names[index[i]];
        }
        return str;
    }

    //day6 贪心算法练习870力扣 优势洗牌
    public int[] advantageCount(int[] nums1, int[] nums2) {
        // 1.排序
        Arrays.sort(nums1);
        int n = nums1.length;
        Integer[] index2 = new Integer[n];
        for (int i = 0; i < n; i++) {
            index2[i] = i;
        }
        Arrays.sort(index2, (i, j) -> {
            return nums2[i] - nums2[j];
        });
        // 2.比较
        int left = 0, right = n - 1;
        int[] ret = new int[n];
        for (int i = 0; i < n; i++) {
            if (nums1[i] > nums2[index2[left]]) {
                ret[index2[left]] = nums1[i];
                left++;
            } else {
                ret[index2[right]] = nums1[i];
                right--;
            }

        }
        return ret;
    }
}
